package account

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"insighttracking/apps/customer/auth"
	"insighttracking/query/dao"
	"net/http"

	"insighttracking/apps/customer/internal/svc"
	"insighttracking/apps/customer/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type ModifyAvatarApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewModifyAvatarApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *ModifyAvatarApiLogic {
	hostname, _ := os.Hostname()
	return &ModifyAvatarApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *ModifyAvatarApiLogic) ModifyAvatarApi(req *types.ModifyAvatarReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	if _, err = dao.AccountEntity.WithContext(l.ctx).Where(dao.AccountEntity.AccountID.Eq(accountInfo.AccountId)).UpdateSimple(
		dao.AccountEntity.Avatar.Value(req.Avatar),
	); err != nil {
		return "", errors.New("修改失败")
	}
	return "修改成功", nil
}
